package com.yida.wy.common.service.impl;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yida.common.core.utils.DateUtils;
import com.yida.wy.common.domain.WyImpSqe;
import com.yida.wy.common.mapper.WyImpSqeMapper;
import com.yida.wy.common.service.IWyImpSqeService;

/**
 * 公维金导入自增序列Service业务层处理
 * 
 * @author hmh
 * @date 2022-12-22
 */
@Service
public class WyImpSqeServiceImpl implements IWyImpSqeService {
    @Autowired
    private WyImpSqeMapper gwjImpSqeMapper;

    Lock lock = new ReentrantLock();

    /**
     * 查询公维金导入自增序列
     * 
     * @param id 公维金导入自增序列主键
     * @return 公维金导入自增序列
     */
    @Override
    public WyImpSqe selectGwjImpSqeById(Long id) {
        return gwjImpSqeMapper.selectGwjImpSqeById(id);
    }

    @Override
    public WyImpSqe selectGwjImpSqeByName(String sqeName) {
        return gwjImpSqeMapper.selectGwjImpSqeByName(sqeName);
    }

    /**
     * 查询公维金导入自增序列列表
     * 
     * @param gwjImpSqe 公维金导入自增序列
     * @return 公维金导入自增序列
     */
    @Override
    public List<WyImpSqe> selectGwjImpSqeList(WyImpSqe gwjImpSqe) {
        return gwjImpSqeMapper.selectGwjImpSqeList(gwjImpSqe);
    }

    /**
     * 新增公维金导入自增序列
     * 
     * @param gwjImpSqe 公维金导入自增序列
     * @return 结果
     */
    @Override
    public int insertGwjImpSqe(WyImpSqe gwjImpSqe) {
        gwjImpSqe.setCreateTime(DateUtils.getNowDate());
        return gwjImpSqeMapper.insertGwjImpSqe(gwjImpSqe);
    }

    /**
     * 修改公维金导入自增序列
     * 
     * @param gwjImpSqe 公维金导入自增序列
     * @return 结果
     */
    @Override
    public int updateGwjImpSqe(WyImpSqe gwjImpSqe) {
        gwjImpSqe.setUpdateTime(DateUtils.getNowDate());
        return gwjImpSqeMapper.updateGwjImpSqe(gwjImpSqe);
    }

    /**
     * 批量删除公维金导入自增序列
     * 
     * @param ids 需要删除的公维金导入自增序列主键
     * @return 结果
     */
    @Override
    public int deleteGwjImpSqeByIds(Long[] ids) {
        return gwjImpSqeMapper.deleteGwjImpSqeByIds(ids);
    }

    /**
     * 删除公维金导入自增序列信息
     * 
     * @param id 公维金导入自增序列主键
     * @return 结果
     */
    @Override
    public int deleteGwjImpSqeById(Long id) {
        return gwjImpSqeMapper.deleteGwjImpSqeById(id);
    }

    @Override
    public Long reserveSqeByIndex(String sqeName, Long index) {
        Long result = -1l;
        try {
            lock.lock();
            // 获取当前序列值
            WyImpSqe gwjImpSqe = selectGwjImpSqeByName(sqeName);
            result = gwjImpSqe.getSequenceNum();
            // 获取预定序列
            Long upSqe = result + index;
            gwjImpSqe.setSequenceNum(upSqe);
            // 更新序列
            gwjImpSqe.setUpdateBy("admin");
            gwjImpSqe.setRemark(result + "change to" + upSqe);
            updateGwjImpSqe(gwjImpSqe);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return result;
    }

}
